package com.njust.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.njust.domain.dto.ProcedureDTO;
import com.njust.domain.po.Message;
import com.njust.domain.po.Procedure;
import com.njust.domain.po.ProcedurePDF;
import com.njust.domain.vo.PageQueryVO;
import com.njust.exception.PDFCountOutOfRange;
import com.njust.exception.ProcedureNotExist;
import com.njust.exception.WithoutProcedurePDF;
import com.njust.mapper.ProcedureMapper;
import com.njust.mapper.ProcedurePDFMapper;
import com.njust.service.ProcedureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.Inet4Address;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @Author Imak
 * @Date 2024/11/15 16:56
 * @规程
 */
@Service
@Slf4j
public class ProcedureServiceImpl implements ProcedureService {
    @Autowired
    private ProcedureMapper procedureMapper;

    @Autowired
    private ProcedurePDFMapper procedurePDFMapper;
    /**
     * 根据项目id查询所有规程 无需分页
     * @param project_id
     * @return
     */
    public PageQueryVO getProcedure(int project_id) {
        //先查询procedure表 获得pdf表id
        // 创建QueryWrapper对象用于构建查询条件
        QueryWrapper<Procedure> wrapper = new QueryWrapper<>();
        wrapper.eq("project_id", project_id);

        // 使用wrapper进行查询，获取满足条件的procedurepdfid列表
        List<Integer> procedurePDFList = procedureMapper.selectList(wrapper).stream()
                .map(Procedure::getProcedure_pdf_id)
                .collect(Collectors.toList());
        if (procedurePDFList.isEmpty()) {
            // 记录日志说明没有查询到对应的procedure_pdf_id
            log.warn("根据项目id {} 未查询到对应的procedure_pdf_id", project_id);
            throw new WithoutProcedurePDF("该项目没有任何规程记录！");
        }

        //再根据id的列表查询
        List<ProcedurePDF> records = procedurePDFMapper.selectBatchIds(procedurePDFList);
        long total = records.size();
        return new PageQueryVO(total, records);
    }

    /**
     * 删除规程pdf
     * @param procedure_pdf_id
     */
    public void deleteProcedure(Integer procedure_pdf_id) {
        procedurePDFMapper.deleteById(procedure_pdf_id);
        // 再清除procedure表中与该procedure_pdf_id相关的数据
        // 创建查询条件Wrapper，用于查找procedure表中与要删除的pdf_id相关的记录
        QueryWrapper<Procedure> wrapper = new QueryWrapper<>();
        wrapper.eq("procedure_pdf_id", procedure_pdf_id);

        // 使用wrapper执行删除操作，删除procedure表中满足条件的记录
        procedureMapper.delete(wrapper);
    }

    /**
     * 规程文件上传
     * @param procedureDTO
     */
    public void uploadProcedure(ProcedureDTO procedureDTO) {

        //先查总的个数，大于4个不能上传
        QueryWrapper<Procedure> wrapper=new QueryWrapper<>();
        wrapper.eq("project_id",procedureDTO.getProject_id());
        long pdf_count=procedureMapper.selectCount(wrapper);
        if(pdf_count>=4){
            throw new PDFCountOutOfRange("规程PDF个数不得超过四个，请先删除存在的规程");
        }
        //往procedure_pdf插入数据
        ProcedurePDF procedurePDF=ProcedurePDF.builder()
                .procedure_pdf_link(procedureDTO.getProcedure_pdf_link())
                .uId(procedureDTO.getUId())
                .build();
        procedurePDFMapper.insert(procedurePDF);

        //往procedure插入数据
        Procedure procedure=Procedure.builder()
                .project_id(procedureDTO.getProject_id())
                .procedure_pdf_id(procedurePDF.getProcedure_pdf_id())
                .build();
        procedureMapper.insert(procedure);

    }

    @Override
    public void delete(Integer procedure_id) {
        Procedure procedure=procedureMapper.selectById(procedure_id);
        if(procedure!=null){
            //删规程表前先删规程pdf
            procedurePDFMapper.deleteById(procedure.getProcedure_pdf_id());
        }else{
            throw new ProcedureNotExist("规程不存在");
        }

        procedureMapper.deleteById(procedure_id);


    }
}
